﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：MessageInfo.aspx.cs
    // 文件功能描述：消息管理：消息详细信息
    // 创建标识：吕海乐2009-5-20
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.MessageManage
{
    public partial class MessageInfo : BasePage
    {

        /// <summary>
        /// 消息的类别
        /// </summary>
        public string MSGType { get; set; }

        public MessageInfo()
        {
            this.ModuleNo = Module.消息管理.ToString();
            this.PageOperateType = OperateType.消息列表.ToString();
        }

        IBLL_Message im = Factory.BLL_Message();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindInfo();
            }
        }

        /// <summary>
        /// 绑定消息或通告的详细信息
        /// </summary>
        private void BindInfo()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["flag"]) && !string.IsNullOrEmpty(Request.QueryString["mID"]))
            {

                int temp;
                //获取消息的类别（M1：消息；N1：公告；N2：待审核的公告）
                string flag = Request.QueryString["flag"];
                string id = Request.QueryString["mID"];
                hdfID.Value = id;
                hdfFlag.Value = flag;

                if (flag == "M1")
                {
                    MSGType = "已收到";

                    Model.View.Message aMessage = im.GetMessage(new HZ.DrugTrade.Model.Query.Message() { MessageID = id }, 1, 0, out temp)[0];
                    tdReciever.InnerText = aMessage.Reciever + "【" + aMessage.ReceiveName + "】";
                    hdfReciever.Value = aMessage.Reciever;
                    tdTitle.InnerText = Server.HtmlEncode(aMessage.Title).ToString();
                    tdSender.InnerText = aMessage.Sender + "【" + aMessage.SenderName + "】";
                    hdfSender.Value = aMessage.Sender;
                    tdSendTime.InnerText = aMessage.SendTime.ToString();
                    tdContent.InnerHtml = aMessage.Content;

                    //回复
                    lblReply.Visible = true;
                    txtReplyContent.Visible = true;
                    btnReply.Visible = true;

                    //若消息状态为未读，则将其更新为已读
                    string status = aMessage.Status.ToString();
                    if (status == "0")
                    {
                        SetMsgStatus(1);
                    }
                }
                else if (flag == "M2")
                {
                    MSGType = "已发送";

                    Model.View.Message aMessage = im.GetMessageSent(new HZ.DrugTrade.Model.Query.Message() { MessageID = id }, 1, 0, out temp)[0];
                    tdReciever.InnerText = aMessage.Reciever + "【" + aMessage.ReceiveName + "】";
                    hdfReciever.Value = aMessage.Reciever;
                    tdTitle.InnerText = Server.HtmlEncode(aMessage.Title).ToString();
                    tdSender.InnerText = aMessage.Sender + "【" + aMessage.SenderName + "】";
                    hdfSender.Value = aMessage.Sender;
                    tdSendTime.InnerText = aMessage.SendTime.ToString();
                    tdContent.InnerHtml = aMessage.Content;

                    //若消息状态为未读，则将其更新为已读
                    string status = aMessage.Status.ToString();
                    if (status == "0")
                    {
                        SetMsgStatus(1);
                    }
                }
            }
            else
            {
                PageError("数据已经不存在，请先选择相应的消息！", this.ReturnUrl);
            }
        }


        /// <summary>
        /// 回复该信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReply_Click(object sender, EventArgs e)
        {
            //回复内容不能为空
            if (string.IsNullOrEmpty(txtReplyContent.Value))
            {
                HZ.Web.WebUtil.Alert("回复内容不能为空！", this);
                return;
            }

            //获取收件人
            int temp;
            IList<Message> lstMsg = new List<Message>();
            Message aMsg = new Message();
            aMsg.Reciever = hdfSender.Value;
            aMsg.MessageID = Guid.NewGuid().ToString();
            aMsg.Status = 0;
            aMsg.Title = "回复：" + tdTitle.InnerText;
            aMsg.Content = txtReplyContent.Value + "<hr><br/>"
            + "------------------ <B>原始信息</B> ------------------<br/> "
            + "<B>发件人:</B>" + CurrentAccount.GetUser().UserID + "【" + CurrentAccount.GetUser().UserName + "】" + "<br/>"
            + "<B>发送时间:</B>" + tdSendTime.InnerText + "<br/>"
            + "<B>收件人:</B>" + tdSender.InnerText + "<br/>"
            + "<B>主题:</B>" + tdTitle.InnerText + "<br/>" + tdContent.InnerHtml;

            lstMsg.Add(aMsg);

            //刷新消息的状态，将原来的消息状态刷新为已回复
            SetMsgStatus(2);

            //回复该消息
            int flag = im.AddMessage_Batch(lstMsg);
            if (flag == 1)
            {
                PageInfo("回复成功！", "MessageGet.aspx");
            }
            else
            {
                PageError("回复失败！", this.ReturnUrl);
            }
        }

        /// <summary>
        /// 刷新消息的状态
        /// </summary>
        /// <param name="status">1：已阅读；2：已回复</param>
        private void SetMsgStatus(int status)
        {
            Message aMsg = new Message();
            aMsg.MessageID = hdfID.Value;
            aMsg.Status = status;

            //等待刷新消息状态的接口
            int flag = im.UpdateMessage(aMsg);
            if (flag != 1)
            {
                PageError("消息状态更新失败！", this.ReturnUrl);
            }

        }


        /// <summary>
        /// 删除消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDel_Click(object sender, EventArgs e)
        {

            IList<Message> lstMsg = new List<Message>();
            lstMsg.Add(new Message() { MessageID = hdfID.Value });
            int flag = im.DeleteMessage_Batch(lstMsg);
            if (flag == 1)
            {
                PageInfo("删除成功！", "MessageGet.aspx");
            }
            else
            {
                PageError("删除失败！", this.ReturnUrl);
            }


        }
    }
}
